package com.example.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.auth.domain.User;
import com.example.config.exception.GlobalExcetion;
import com.example.domain.BusNote;
import com.example.domain.dto.BusNoteDto;
import com.example.feign.UserFeignService;
import com.example.service.BusNoteService;
import com.example.util.Results;
import com.example.domain.vo.BusNoteVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/note")
public class NoteController {

    @Autowired
    private BusNoteService busNoteService;

    @Autowired
    private UserFeignService userFeignService;

    @PostMapping("/saveorupdate")
    public JSONObject saveOrUpdate(@RequestBody @Validated BusNoteVo vo) throws GlobalExcetion {
        BusNote busNote=busNoteService.saveOrUpdateBusNote(vo);
        return Results.success(busNote);
    }

    @GetMapping("/getone")
    public JSONObject getOne(@RequestParam("noteId") String noteId) throws GlobalExcetion {
        BusNote busNote=busNoteService.getBusNoteCache(noteId);
        return Results.success(busNote);
    }

    @GetMapping("/getoneinfo")
    public JSONObject getOneInfo(@RequestParam("noteId") String noteId) throws GlobalExcetion {
        BusNoteDto busNoteDto=busNoteService.getBusNoteDtoCache(noteId);
        return Results.success(busNoteDto);
    }

    @GetMapping("/page/user")
    public JSONObject page(@RequestParam("userId")String userId,
                           @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                           @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
                           @RequestParam(value = "keyword",required = false)String keyword) throws GlobalExcetion {
        User user=userFeignService.getUserByUserId(userId);
        if(user==null){
            return Results.error("用户id不存在");
        }
        IPage<BusNote> busNoteIPage=busNoteService.pageBusNote(userId,pageNum,pageSize,keyword);
        return Results.success(busNoteIPage);
    }
}
